﻿using System;
using System.Collections.Generic;
using System.Linq;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;

namespace T.FAS.Runtime.DataAccess.UnitTest
{
    public class DataQueryModelBuilder
    {
        public const string Dept_DataObjectid = "86101735-4C3A-4E22-9DEB-F2A7BEB561B6";
        public const string User_DataObjectid = "34162DBD-E812-456E-9FD8-7C6C52DFB233";
        public const string DatabaseID = "TTestDB";
        /// <summary>
        /// 租户id
        /// </summary>
        public const string TenantID = "2467d67d-ec47-11eb-b73c-0017fa000887";

        public static DataQuery BuildDQMetadata_Simple()
        {
            var dep_do = CreateDO_DeptEntity();
            var user_do = CreateDO_UserEntity();

            DataQuery dataQuery = new DataQuery
            {
                ID = "GetDepUserList",
                Code = "GetDepUserList",
                Description = "DepartQuery",
                //IsDistinct = true,
                Timeout = 123,
                CacheInMinutes = 123
            };
            dataQuery.DataQuerySources = new List<DataQuerySource>
            {
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = user_do.ID,
                    DataObject = user_do,
                    IsMainSource = true,
                    Sort = 0,
                    Alias = "DO_User",
                },
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dep_do.ID,
                    DataObject = dep_do,
                    IsMainSource = false,
                    Sort = 1,
                    Alias = "DO_Dept",
                }
            };
            dataQuery.DataQueryJoins = new List<DataQueryJoin>();

            var join = new DataQueryJoin();
            join.ID = Guid.NewGuid().ToString();
            join.LeftSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User").ID;
            join.LeftQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User");    //必须
            join.RightSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept").ID;
            join.RightQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept");//必须
            join.JoinType = 0;
            join.JoinExpression = "DO_Dept.ID=DO_User.DepID";
            join.JoinExpressionJson = "222";

            dataQuery.DataQueryJoins.Add(join);
            dataQuery.DataQueryColumns = new List<DataQueryColumn>
            {
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID").ID,
                    Name = "用户标识",
                    Alias = "UserID",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code").ID,
                    Name = "用户编号",
                    Alias = "UserCode",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    Name = "用户名称",
                    Alias = "UserName",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age").ID,
                    Name = "年龄",
                    Alias = "Age",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Sexy"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Sexy").ID,
                    Name = "性别",
                    Alias = "Sexy",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").DataObject, //必须
                    Name = "部门信息",
                    Alias = "DeptInfo",
                     ColumnExpression = "DO_Dept.Code+DO_Dept.Name",
                    ColumnType =  ColumnType.Express,
                    Sort = 1,
                },
            };
            dataQuery.DataQueryFilters = new List<DataQueryFilter>
            {
                new DataQueryFilter
                {
                    ID = Guid.NewGuid().ToString(),
                    FilterExpression = "DO_User.Code like '@input1%'  or DO_User.Name like '@input1%'",
                    FilterExpressionJson = "2-2-2",
                    Sort = 1,
                }
            };
            dataQuery.DataQuerySorts = new List<DataQuerySort>
            {
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code").ID,
                    SortType = 1,
                    Sort = 2,
                },
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    SortType = 1,
                    Sort = 2,
                },
            };
            dataQuery.DataQueryParams = new List<DataQueryParam>
            {
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "input1",
                    Name = "input1",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = "string",
                    DefaultValue = "",
                    ParamValue = "张三",
                    ExpressJson = "",
                    IsAllowNull = true,
                    Sort = 0,
                }
            };

            return dataQuery;
        }

        public static DataQuery BuildDQMetadata_SimpleWithColExpression()
        {
            var dep_do = CreateDO_DeptEntity();
            var user_do = CreateDO_UserEntity();

            DataQuery dataQuery = new DataQuery
            {
                ID = "GetDepUserList",
                Code = "GetDepUserList",
                Description = "DepartQuery",
                Timeout = 123,
                CacheInMinutes = 123
            };
            dataQuery.DataQuerySources = new List<DataQuerySource>
            {
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = user_do.ID,
                    DataObject = user_do,
                    IsMainSource = true,
                    Sort = 0,
                    Alias = "DO_User",
                },
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dep_do.ID,
                    DataObject = dep_do,
                    IsMainSource = false,
                    Sort = 1,
                    Alias = "DO_Dept",
                }
            };
            dataQuery.DataQueryJoins = new List<DataQueryJoin>();

            var join = new DataQueryJoin();
            join.ID = Guid.NewGuid().ToString();
            join.LeftSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User").ID;
            join.LeftQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User");    //必须
            join.RightSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept").ID;
            join.RightQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept");//必须
            join.JoinType = 0;
            join.JoinExpression = "DO_Dept.ID=DO_User.DepID";
            join.JoinExpressionJson = "222";

            dataQuery.DataQueryJoins.Add(join);
            dataQuery.DataQueryColumns = new List<DataQueryColumn>
            {
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID").ID,
                    Name = "用户标识",
                    Alias = "UserID",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code").ID,
                    Name = "用户编号",
                    Alias = "UserCode",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    Name = "用户名称",
                    Alias = "UserName",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age").ID,
                    Name = "年龄",
                    Alias = "Age",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Sexy"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Sexy").ID,
                    Name = "性别",
                    Alias = "Sexy",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").DataObject, //必须
                    Name = "部门信息",
                    Alias = "DeptInfo",
                     ColumnExpression = "DO_Dept.Code+DO_Dept.Name",
                    ColumnType =  ColumnType.Express,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    Name = "固定部门信息",
                    Alias = "ConstInfo",
                     ColumnExpression = "\"研发\"",
                    ColumnType =  ColumnType.Express,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    Name = "固定部门信息-无",
                    Alias = "ConstInfoNone",
                     ColumnExpression = "",
                    ColumnType =  ColumnType.Express,
                    Sort = 1,
                },
               new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").DataObject, //必须
                    Name = "数量",
                    Alias = "ConstCount",
                   DataColumn = dep_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID"),
                    ColumnType =  ColumnType.Aggregate,
                    AggregateType= AggregateType.COUNT,
                    Sort = 1,
                },
            };
            dataQuery.DataQueryFilters = new List<DataQueryFilter>
            {
                new DataQueryFilter
                {
                    ID = Guid.NewGuid().ToString(),
                    FilterExpression = "DO_User.Code like '@input1%'  or DO_User.Name like '@input1%'",
                    FilterExpressionJson = "2-2-2",
                    Sort = 1,
                }
            };
            dataQuery.DataQuerySorts = new List<DataQuerySort>
            {
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code").ID,
                    SortType = 1,
                    Sort = 2,
                },
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    SortType = 1,
                    Sort = 2,
                },
            };
            dataQuery.DataQueryParams = new List<DataQueryParam>
            {
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "input1",
                    Name = "input1",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = "string",
                    DefaultValue = "",
                    ParamValue = "张三",
                    ExpressJson = "",
                    IsAllowNull = true,
                    Sort = 0,
                }
            };

            return dataQuery;
        }

        public static DataQuery BuildDQMetadata_GroupAndHaving()
        {
            var dep_do = CreateDO_DeptEntity();
            var user_do = CreateDO_UserEntity();

            DataQuery dataQuery = new DataQuery
            {
                ID = "GetDepUserList",
                Code = "GetDepUserList",
                Description = "DepartQuery",
                Timeout = 123,
                CacheInMinutes = 123
            };
            dataQuery.DataQuerySources = new List<DataQuerySource>
            {
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = user_do.ID,
                    DataObject = user_do,
                    IsMainSource = true,
                    Sort = 0,
                    Alias = "DO_User",
                },
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dep_do.ID,
                    DataObject = dep_do,
                    IsMainSource = false,
                    Sort = 1,
                    Alias = "DO_Dept",
                }
            };
            dataQuery.DataQueryJoins = new List<DataQueryJoin>();

            var join = new DataQueryJoin();
            join.ID = Guid.NewGuid().ToString();
            join.LeftSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User").ID;
            join.LeftQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User");    //必须
            join.RightSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept").ID;
            join.RightQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept");//必须
            join.JoinType = 0;
            join.JoinExpression = "DO_User.DepID=DO_Dept.ID";

            dataQuery.DataQueryJoins.Add(join);
            dataQuery.DataQueryColumns = new List<DataQueryColumn>
            {
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID").ID,
                    Name = "用户标识",
                    Alias = "UserIDCount",
                    AggregateType = AggregateType.COUNT,
                    ColumnType =  ColumnType.Aggregate,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age").ID,
                    Name = "年龄",
                    Alias = "Age",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age").ID,
                    Name = "年龄",
                    Alias = "GroupAge",
                    ColumnType =  ColumnType.Group,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    Name = "姓名",
                    Alias = "UserName",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
            };

            dataQuery.DataQueryFilters = new List<DataQueryFilter>
            {
                new DataQueryFilter
                {
                    ID = Guid.NewGuid().ToString(),
                    FilterExpression = "DO_User.Age > @minAgeParam ",
                    Sort =2,
                },
                new DataQueryFilter
                {
                    ID = Guid.NewGuid().ToString(),
                    FilterExpression = "DO_User.Age < @maxAgeParam",
                    Sort = 3,
                },
                new DataQueryFilter
                {
                    ID = Guid.NewGuid().ToString(),
                    FilterExpression = "DO_User.Sexy = @sexyParam",
                    Sort = 1,
                },
                new DataQueryFilter
                {
                    ID = Guid.NewGuid().ToString(),
                     FilterType= FilterType.Aggregate,
                      
                    FilterExpression = "COUNT(DO_User.ID)  > 1 ",
                    Sort = 1,
                },
            };
            dataQuery.DataQuerySorts = new List<DataQuerySort>
            {
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code").ID,
                    SortType = 1,
                    Sort = 2,
                },
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    SortType = 1,
                    Sort = 2,
                },
            };
            dataQuery.DataQueryParams = new List<DataQueryParam>
            {
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "minAgeParam",
                    Name = "minAgeParam",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = "int",
                    ParamValue = "0",
                    IsAllowNull = true,
                    Sort = 0,
                },
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "maxAgeParam",
                    Name = "maxAgeParam",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = "int",
                    ParamValue = "100",
                    IsAllowNull = true,
                    Sort = 1,
                },
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "sexyParam",
                    Name = "sexyParam",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = "int",
                    ParamValue = "1",
                    IsAllowNull = true,
                    Sort = 2,
                }
            };

            return dataQuery;
        }

        public static DataQuery BuildDQMetadata_ManyParam()
        {
            var dep_do = CreateDO_DeptEntity();
            var user_do = CreateDO_UserEntity();

            DataQuery dataQuery = new DataQuery
            {
                ID = "GetDepUserList",
                Code = "GetDepUserList",
                Description = "DepartQuery",
                //IsDistinct = true,
                Timeout = 123,
                CacheInMinutes = 123
            };
            dataQuery.DataQuerySources = new List<DataQuerySource>
            {
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = user_do.ID,
                    DataObject = user_do,
                    IsMainSource = true,
                    Sort = 0,
                    Alias = "DO_User",
                },
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dep_do.ID,
                    DataObject = dep_do,
                    IsMainSource = false,
                    Sort = 1,
                    Alias = "DO_Dept",
                }
            };
            dataQuery.DataQueryJoins = new List<DataQueryJoin>();

            var join = new DataQueryJoin();
            join.ID = Guid.NewGuid().ToString();
            join.LeftSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User").ID;
            join.LeftQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User");    //必须
            join.RightSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept").ID;
            join.RightQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept");//必须
            join.JoinType = 0;
            join.JoinExpression = "DO_Dept.ID=DO_User.DepID";
            join.JoinExpressionJson = "222";

            dataQuery.DataQueryJoins.Add(join);
            dataQuery.DataQueryColumns = new List<DataQueryColumn>
            {
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID").ID,
                    Name = "用户标识",
                    Alias = "UserID",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code").ID,
                    Name = "用户编号",
                    Alias = "UserCode",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    Name = "用户名称",
                    Alias = "UserName",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age").ID,
                    Name = "年龄",
                    Alias = "Age",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Sexy"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Sexy").ID,
                    Name = "性别",
                    Alias = "Sexy",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_Dept").DataObject, //必须
                    Name = "部门信息",
                    Alias = "DeptInfo",
                     ColumnExpression = "DO_Dept.Code+DO_Dept.Name",
                    ColumnType =  ColumnType.Express,
                    Sort = 1,
                },
            };
            dataQuery.DataQueryFilters = new List<DataQueryFilter>
            {
                new DataQueryFilter
                {
                    ID = Guid.NewGuid().ToString(),
                    FilterExpression = "DO_User.Code like '@input1%'  or DO_User.Name like '@input1%'",
                    FilterExpressionJson = "2-2-2",
                    Sort = 1,
                }
            };
            dataQuery.DataQuerySorts = new List<DataQuerySort>
            {
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code").ID,
                    SortType = 1,
                    Sort = 2,
                },
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    SortType = 1,
                    Sort = 2,
                },
            };
            dataQuery.DataQueryParams = new List<DataQueryParam>
            {
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "String",
                    Name = "String",
                    IsInputParam = true,
                    ParamType = DataType.String.ToString(),
                    ParamValue = "张三",
                    IsAllowNull = true,
                    Sort = 0,
                },
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "Boolean",
                    Name = "Boolean",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = DataType.Boolean.ToString(),
                    DefaultValue = "",
                    ParamValue = "张三",
                    ExpressJson = "",
                    IsAllowNull = true,
                    Sort = 1,
                },
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "Int",
                    Name = "Int",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = DataType.Int.ToString(),
                    DefaultValue = "",
                    ParamValue = "张三",
                    ExpressJson = "",
                    IsAllowNull = true,
                    Sort = 2,
                }
            };

            return dataQuery;
        }

        public static DataQuery BuildDQMetadata_Aggregate()
        {
            var dep_do = CreateDO_DeptEntity();
            var user_do = CreateDO_UserEntity();

            DataQuery dataQuery = new DataQuery
            {
                ID = "GetDepUserList",
                Code = "GetDepUserList",
                Description = "DepartQuery",
                Timeout = 123,
                CacheInMinutes = 123
            };
            dataQuery.DataQuerySources = new List<DataQuerySource>
            {
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = user_do.ID,
                    DataObject = user_do,
                    IsMainSource = true,
                    Sort = 0,
                    Alias = "DO_User",
                },
                new DataQuerySource
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dep_do.ID,
                    DataObject = dep_do,
                    IsMainSource = false,
                    Sort = 1,
                    Alias = "DO_Dept",
                }
            };
            dataQuery.DataQueryJoins = new List<DataQueryJoin>();

            var join = new DataQueryJoin();
            join.ID = Guid.NewGuid().ToString();
            join.LeftSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User").ID;
            join.LeftQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_User");    //必须
            join.RightSourceID = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept").ID;
            join.RightQuerySource = dataQuery.DataQuerySources.FirstOrDefault(o => o.Alias == "DO_Dept");//必须
            join.JoinType = 0;
            join.JoinExpression = "DO_User.DepID=DO_Dept.ID";

            dataQuery.DataQueryJoins.Add(join);
            dataQuery.DataQueryColumns = new List<DataQueryColumn>
            {
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="ID").ID,
                    Name = "用户标识",
                    Alias = "UserIDCount",
                    AggregateType = AggregateType.COUNT,
                    ColumnType =  ColumnType.Aggregate,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                     DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age"),
                     DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Age").ID,
                    Name = "年龄",
                    Alias = "Age",
                     AggregateType = AggregateType.MAX,
                    ColumnType =  ColumnType.Aggregate,
                    Sort = 1,
                },
                new DataQueryColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    DataQuerySourceID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObjectID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    Name = "姓名",
                    Alias = "UserName",
                    ColumnType =  ColumnType.Normal,
                    Sort = 1,
                },
            };

            dataQuery.DataQueryFilters = new List<DataQueryFilter>
            {
                //new DataQueryFilter
                //{
                //    ID = Guid.NewGuid().ToString(),
                //    FilterExpression = "DO_User.Age > @minAgeParam ",
                //    Sort =2,
                //},
                //new DataQueryFilter
                //{
                //    ID = Guid.NewGuid().ToString(),
                //    FilterExpression = "DO_User.Age < @maxAgeParam",
                //    Sort = 3,
                //},
                //new DataQueryFilter
                //{
                //    ID = Guid.NewGuid().ToString(),
                //    FilterExpression = "DO_User.Sexy = @sexyParam",
                //    Sort = 1,
                //},
                //new DataQueryFilter
                //{
                //    ID = Guid.NewGuid().ToString(),
                //     FilterType= FilterType.Aggregate,

                //    FilterExpression = "COUNT(DO_User.ID)  > 1 ",
                //    Sort = 1,
                //},
            };
            dataQuery.DataQuerySorts = new List<DataQuerySort>
            {
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Code").ID,
                    SortType = 1,
                    Sort = 2,
                },
                new DataQuerySort
                {
                    ID = Guid.NewGuid().ToString(),
                    DataObjectID = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").ID,
                    DataObject = dataQuery.DataQuerySources.Find(o => o.Alias == "DO_User").DataObject, //必须
                    DataColumn = user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name"),
                    DataObjectColumnID=user_do.Columns.FirstOrDefault(x=>x.ColumnName=="Name").ID,
                    SortType = 1,
                    Sort = 2,
                },
            };
            dataQuery.DataQueryParams = new List<DataQueryParam>
            {
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "minAgeParam",
                    Name = "minAgeParam",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = "int",
                    ParamValue = "0",
                    IsAllowNull = true,
                    Sort = 0,
                },
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "maxAgeParam",
                    Name = "maxAgeParam",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = "int",
                    ParamValue = "100",
                    IsAllowNull = true,
                    Sort = 1,
                },
                new DataQueryParam
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = "sexyParam",
                    Name = "sexyParam",
                    IsExpr = false,
                    IsInputParam = true,
                    ParamType = "int",
                    ParamValue = "1",
                    IsAllowNull = true,
                    Sort = 2,
                }
            };

            return dataQuery;
        }

        /// <summary>
        /// 获取DB实例
        /// </summary>
        /// <param name="databaseID">修改DB的主键</param>
        /// <returns></returns>
        private static Database CreateDatabaseEntity(string dbID = DatabaseID)
        {
            #region Construct Database Entity  
            Database database = new Database
            {
                ID = string.IsNullOrEmpty(dbID) ? Guid.NewGuid().ToString() : dbID,
                Code = "TTest",
                Name = "特来电测试库",
                DisplayName = "特来电测试库",
                Description = "特来电测试库说明",
                IsDeleted = true,
                IsSharding = true,
                DbType = "MySQL",
                Charset = "utf8mb4",
                Visibility = 0,
                PersonInCharge = "Illidan",
                TenantID = TenantID,
                Ext1 = "EXT1",
                Ext2 = "EXT2",
                Ext3 = "EXT3",
                Creator = "Tyrande",
                CreateTime = DateTime.Now,
                LastModifier = "Jaina",
                LastModifyTime = DateTime.Now,
            };
            database.DatabaseConfigs = new List<DatabaseConfig>
                {
                    new DatabaseConfig
                    {
                        DatabaseID = database.ID,
                        ConfigKey = "TBoss_Local",
                        ConfigName = "TMaster连接字符串",
                        Mode = 1,
                        Sort = 1,
                        IsDefault = true,
                        Ext1 = "Ext1",
                        Ext2 = "Ext2",
                        Ext3 = "Ext3",
                        Creator = "Anduin",
                        CreateTime = DateTime.Now,
                        LastModifier = "Anduin",
                        LastModifyTime = DateTime.Now,
                    },
                    new DatabaseConfig
                    {
                        DatabaseID = database.ID,
                        ConfigKey = "TTP.Database.TMasterMySQL.ConnectionString",
                        ConfigName = "TMaster MySQL连接字符串",
                        Mode = 0,
                        Sort = 2,
                        IsDefault = false,
                        Ext1 = "Ext1",
                        Ext2 = "Ext2",
                        Ext3 = "Ext3",
                        Creator = "Sylvanas",
                        CreateTime = DateTime.Now,
                        LastModifier = "Sylvanas",
                        LastModifyTime = DateTime.Now,
                    }
            };
            #endregion
            return database;
        }

        private static DataObject CreateDO_DeptEntity(string DataObjectID = Dept_DataObjectid, string userDOID = User_DataObjectid)
        {
            #region Construct DataObject Entity  
            DataObject DataObject = new DataObject
            {
                ID = DataObjectID,
                Code = "DO_Dept",
                Descriptions = "部门",
                DatabaseID = DatabaseID,
                LogicTableName = "test_dept",
                ActualTableNames = "test_dept",
                IsMainData = false,
                IsReference = false,
                RefDataObjectID = null,
                EnableFulldoseUpdate = true,
                EnableEvent = true,
                EnableAuditLog = true,
                EnableLogicalDelete = true,
                Database = CreateDatabaseEntity()
            };
            DataObject.Columns = new List<DataColumn>
            {
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "ID",
                    DisplayName= "用户id",
                    Description="用户id说明",
                    DataType= "string",
                    RelatedDataObjectID= "DO_User",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= true,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "Code",
                    DisplayName= "编号",
                    Description="编号",
                    DataType= "string",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "Name",
                    DisplayName= "用户名",
                    Description="用户名说明",
                    DataType= "string",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "LastModifyUser",
                    DisplayName= "最后修改人",
                    Description="最后修改人",
                    DataType= "string",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "LastModifyTime",
                    DisplayName= "最后修改时间",
                    Description="最后修改时间",
                    DataType= "datetime",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= true,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "CreateUser",
                    DisplayName= "创建人",
                    Description="创建人",
                    DataType= "string",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "CreateTime",
                    DisplayName= "创建时间",
                    Description="创建时间",
                    DataType= "datetime",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                 new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "PID",
                    DisplayName= "父部门ID",
                    Description="父部门ID",
                    DataType= "string",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= true,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                    RelatedDataObjectID =DataObjectID
                },
                 new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "UserID",
                    DisplayName= "员工标记",
                    Description="员工标记",
                    DataType= "string",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                    RelatedDataObjectID =userDOID
                },
            };

            DataObject.DataObjectRelations = new List<DataObjectRelation>
            {
                new DataObjectRelation
                {
                    ID= Guid.NewGuid().ToString(),
                    FromDoID= DataObjectID,
                    ToColumnID= DataObject.Columns.Find(col => col.ColumnName == "PID").ID,
                    RelationType= 1,	//关联类型 0:一对多 1:一对一
			        DeleteRule= 1,//删除规则:0:ignore; 1:delete; 2:protected
                },
                new DataObjectRelation
                {
                    ID= Guid.NewGuid().ToString(),
                    FromDoID= userDOID,
                    ToColumnID= DataObject.Columns.Find(col => col.ColumnName == "UserID").ID,
                    RelationType= 1,	//关联类型 0:一对多 1:一对一
			        DeleteRule= 1,//删除规则:0:ignore; 1:delete; 2:protected
                },
            };
            #endregion

            return DataObject;
        }

        private static DataObject CreateDO_UserEntity(string DataObjectID = User_DataObjectid, string depID = Dept_DataObjectid)
        {
            DataObject DataObject = new DataObject
            {
                ID = DataObjectID,
                Descriptions = "用户",
                Code = "DO_User",
                DatabaseID = DatabaseID,
                LogicTableName = "test_user",
                ActualTableNames = "test_user",
                IsMainData = false,
                IsReference = false,
                EnableFulldoseUpdate = true,
                EnableEvent = true,
                EnableAuditLog = true,
                EnableLogicalDelete = true,
                Database = CreateDatabaseEntity()
            };
            DataObject.Columns = new List<DataColumn>
            {
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "ID",
                    DisplayName= "用户id",
                    Description="用户id说明",
                    DataType= "string",
                    RelatedDataObjectID= "DO_User",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= true,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "Code",
                    DisplayName= "编号",
                    Description="编号",
                    DataType= "string",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "Name",
                    DisplayName= "用户名",
                    Description="用户名说明",
                    DataType= "varchar(max)",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "LastModifyUser",
                    DisplayName= "最后修改人",
                    Description="最后修改人",
                    DataType= "string",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "LastModifyTime",
                    DisplayName= "最后修改时间",
                    Description="最后修改时间",
                    DataType= "datetime",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= true,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "CreateUser",
                    DisplayName= "创建人",
                    Description="创建人",
                    DataType= "string",
                    //RelatedDataObjectID= "当本列为外键列时(即DataType=DataObject时)，本字段保存外键关联DOid",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "CreateTime",
                    DisplayName= "创建时间",
                    Description="创建时间",
                    DataType= "datetime",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "Sexy",
                    DisplayName= "性别",
                    Description="性别",
                    DataType= "int",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "Age",
                    DisplayName= "年龄",
                    Description="年龄",
                    DataType= "int",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= false,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                },
                new DataColumn
                {
                    ID = Guid.NewGuid().ToString(),
                    ColumnName= "DepID",
                    DisplayName= "部门ID",
                    Description="部门ID",
                    DataType= "varchar(max)",
                    Length= 200,	//长度
			        Precision= 1,	//精度
			        DefaultValue= "''",
                    ColumnOrder= 1,	//列排序
			        IsNullable= true,	//是否可空
			        IsPkColumn= false,	//是否主键
			        IsFkColumn= true,	//是否外键
			        IsAudit= true,		//是否审计字段
			        IsEncrypted= false,	//是否加密字段
			        EncryptRule= 1,		//加密规则
                    RelatedDataObjectID =depID
                },
            };

            return DataObject;
        }
    }
}
